﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using elfam.web.Models;
using elfam.web.Services;
using elfam.web.Services.Results;
using elfam.web.ViewModels;
using elfam.web.ViewModels.Cart;
using elfam.web.ViewModels.User;
using Microsoft.Practices.Unity;

namespace elfam.web.Controllers
{
    public class CartController : BaseController
    {
        private UserService userService = new UserService();
        private OrderService orderService;
         
        [Dependency]
        public OrderService OrderService
        {
            set { orderService = value; }
        }
        
        public ActionResult Index(Cart cart)
        {
            return View("Review", new ReviewCartViewModel{ Cart = cart});
        }



        public ActionResult Add(Cart cart, int productId, int quantity)
        {
            Product product = daoTemplate.FindByID<Product>(productId);
            if (product.Rest() < quantity)
            {
                return Json(new [] {new { Key = "quantity", Value = "Слишком большое количество" }});
            }
            cart.Add(product, quantity);
            return Json(new object[]{});
        }

        public ActionResult CartWidget(Cart  cart)
        {
            return View(cart);
        }

        [HttpPost]
        public ActionResult Recalc(EditAndCheckoutViewModel viewModel, Cart cart)
        {
            // take products from cart and update quantity
            foreach (var item in viewModel.CartLines)
            {
                CartLineViewModel item1 = item;
                cart.Items.Single(x => x.Product.Id == item1.ProductId).Quantity = item.Quantity;
            }
            int i = 0;
            foreach (CartItem cartItem in cart.Items)
            {
                cartItem.Product = daoTemplate.FindByID<Product>(cartItem.Product.Id);
                if (cartItem.Product.Rest() < cartItem.Quantity)
                    ModelState.AddModelError(string.Format("CartLines[{0}].Quantity", i), "Слишком большое количество");
                i++;
            }
            return RedirectToAction("/");
        }

        [HttpPost]
        public ActionResult Checkout(EditAndCheckoutViewModel viewModel, Cart cart)
        {
            // take products from cart and update quantity
            foreach (var item in viewModel.CartLines)
            {
                CartLineViewModel item1 = item;
                cart.Items.Single(x => x.Product.Id == item1.ProductId).Quantity = item.Quantity;
            }

            if (CurrentUser() == null)
            {
                return View("LoginOrRegister");
            }

            int i = 0;
            foreach (CartItem cartItem in cart.Items)
            {
                cartItem.Product = daoTemplate.FindByID<Product>(cartItem.Product.Id);
                if (cartItem.Product.Rest() < cartItem.Quantity)
                    ModelState.AddModelError(string.Format("CartLines[{0}].Quantity", i), "Слишком большое количество");
                i++;
            }
            if (!ModelState.IsValid)
            {
                return View("Review", new ReviewCartViewModel
                {
                    Cart = cart
                });
            }
            return RedirectToAction("EnterShippingDetails");
        }

        public ActionResult EnterShippingDetails(Cart cart)

        {
            return View("EnterShippingDetails", new EnterShippingDetailsViewModel()
                                                    {
                                                        Contact = ContactViewModel.From(CurrentUser().Contact),
                                                        User = CurrentUser(),
                                                        Cart = cart,
                                                    });
        }

        [HttpPost]
        public ActionResult LoginAndCheckout(string email, string password, Cart cart)
        {
            if (userService.AuthenticateUser(email, password))
            {
                User user = daoTemplate.FindUniqueByField<User>(Models.User.EmailProperty, email);
                FormsAuthentication.SetAuthCookie(email, true);
                return View("EnterShippingDetails", new EnterShippingDetailsViewModel()
                                                        {
                                                            Contact = ContactViewModel.From(user.Contact),
                                                            User = user,
                    Cart = cart,
                });
            }
            else
            {
               ModelState.AddModelError("login", "Неправильное имя пользователя или пароль");
               return View("LoginOrRegister", cart); 
            }
        }

        [HttpPost]
        public ActionResult Send(EnterShippingDetailsViewModel viewModel, Cart cart)
        {
            if (!ModelState.IsValid)
            {
                return View("EnterShippingDetails", new EnterShippingDetailsViewModel()
                {
                    Contact = ContactViewModel.From(CurrentUser().Contact),
                    Cart = cart
                });
            }

            OrderShippingInfo info = OrderShippingInfo.From(viewModel);


            AddOrderResult result = orderService.AddOrder(cart.Items, CurrentUser(), info);
            cart.Items.Clear();
            return RedirectToAction("DetailsUser", "Order", new { id = result.Order.Uid });
        }

        [HttpPost]
        public ActionResult Remove(int productId, Cart cart)
        {
            var remove = cart.Items.Single(x => x.Product.Id == productId);
            cart.Items.Remove(remove);
            return View("Review", new ReviewCartViewModel()
            {
                Cart = cart

            }); 
        }

    }
}
